@{
    ViewBag.Title = "在线文本加密解密,在线聊天内容加密解密工具";
    ViewBag.keywords = "在线文本加密解密,聊天内容加密,聊天内容解密,加密解密工具,在线工具,在线聊天内容加密解密工具";
    ViewBag.description = "这款在线聊天内容加密解密工具可实现针对聊天内容自定义暗号进行加密与解密的功能，加密与解密的聊天内容可以是中英文、字母、数字及特殊符号等";
}
<div class="layui-elem-quote">
    这款聊天内容加密解密工具可实现针对聊天内容自定义暗号进行加密与解密的功能，加密与解密的聊天内容可以是中英文、字母、数字及特殊符号等。
</div>
<div class="layui-row layui-form layui-form-pane">
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">内容：</label>
        <div class="layui-input-block">
            <textarea placeholder="请输入内容" id="content" name="content" rows="8" lay-verify="required" autocomplete="off" class="layui-textarea">www.ruovea.com</textarea>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">暗号：</label>
        <div class="layui-input-block">
            <input type="text" id="ahcontent" name="ahcontent" lay-verify="required" placeholder="请输入内容" autocomplete="off" class="layui-input" value="123456" />
        </div>
    </div>
    <div class="layui-form-item">
        <a class="layui-btn" onclick="encrypt('content','ahcontent','outtxt');">加密</a>
        <a class="layui-btn layui-btn-normal" onclick="decrypt('content','ahcontent','outtxt');">解密</a>
        <a class="layui-btn layui-btn-danger" onclick="cleanup('content','ahcontent','outtxt');">清空</a>
        <a class="layui-btn layui-btn-primary copy" onclick="copytxt();">复制结果</a>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">输出：</label>
        <div class="layui-input-block">
            <textarea placeholder="输出内容" id="outtxt" name="outtxt" rows="8" autocomplete="off" class="layui-textarea"></textarea>
        </div>
    </div>
</div>

@section Scripts{
<script language="JavaScript">
    function utf16to8(str) {
        var out, i, len, c;
        out = "";
        len = str.length;
        for (i = 0; i < len; i++) {
            c = str.charCodeAt(i);
            if ((c >= 0x0001) && (c <= 0x007F)) {
                out += str.charAt(i);
            } else if (c > 0x07FF) {
                out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
                out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
                out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
            } else {
                out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
                out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
            }
        }
        return out;
    }function utf8to16(str) {
        var out, i, len, c;var char2, char3;
        out = "";len = str.length;
        i = 0;
        while (i < len) {
            c = str.charCodeAt(i++);
            switch (c >> 4) {
                case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
                    // 0xxxxxxx
                    out += str.charAt(i - 1);
                    break;
                case 12: case 13:
                    // 110x xxxx 10xx xxxx
                    char2 = str.charCodeAt(i++);
                    out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
                    break;
                case 14:
                    // 1110 xxxx 10xx xxxx 10xx xxxx
                    char2 = str.charCodeAt(i++);char3 = str.charCodeAt(i++);out += String.fromCharCode(((c & 0x0F) << 12) | ((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0));break;}}return out;}function cleanup(content, ahcontent, outtxt) {
        if (content == null) { console.log("content为空."); return; }
        if (ahcontent == null) { console.log("ahcontent为空."); return; }
        if (outtxt == null) { console.log("outcontent为空."); return; }
        document.getElementById(content).value = '';
        //document.getElementById(ahcontent).value = '';
        document.getElementById(outtxt).value = '';}function encrypt(content, pwdcontent, outtxt) {
        if (content == null) { console.log("content为空."); return; }
        if (pwdcontent == null) { console.log("pwdcontent为空."); return; }
        if (outtxt == null) { console.log("outcontent为空."); return; }
        var inttxt = document.getElementById(content).value;
        if (inttxt === undefined || inttxt === '' || inttxt === null) { layer.msg("内容不能为空.", { icon: 2 }); return; }
        var pwd = document.getElementById(pwdcontent).value;
        if (pwd === undefined || pwd === '' || pwd === null) { layer.msg("暗号不能为空.", { icon: 2 }); return; }
        inttxt = utf16to8(inttxt);
        var prand = "";
        for (var i = 0; i < pwd.length; i++) {
            prand += pwd.charCodeAt(i).toString();
        }
        var sPos = Math.floor(prand.length / 5);
        var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos * 2) + prand.charAt(sPos * 3) + prand.charAt(sPos * 4) + prand.charAt(sPos * 5));
        var incr = Math.ceil(pwd.length / 2);
        var modu = Math.pow(2, 31) - 1;
        if (mult < 2) {
            var alue = "Algorithm cannot find a suitable hash. Please choose a different password. \nPossible considerations are to choose a more complex or longer password.";
            layer.msg(alue, { icon: 2 }); return;
        }
        var salt = Math.round(Math.random() * 1000000000) % 100000000;
        prand += salt;
        while (prand.length > 10) {
            prand = (parseInt(prand.substring(0, 10)) + parseInt(prand.substring(10, prand.length))).toString();
        }
        prand = (mult * prand + incr) % modu;
        var enc_chr = "";
        var enc_str = "";
        for (var i = 0; i < inttxt.length; i++) {
            enc_chr = parseInt(inttxt.charCodeAt(i) ^ Math.floor((prand / modu) * 255));
            if (enc_chr < 16) {
                enc_str += "0" + enc_chr.toString(16);
            } else enc_str += enc_chr.toString(16);
            prand = (mult * prand + incr) % modu;
        }
        salt = salt.toString(16);
        while (salt.length < 8) {
            salt = "0" + salt;
            enc_str += salt;
        }
        document.getElementById(outtxt).value = enc_str;
    }function decrypt(content, pwdcontent, outtxt) {
        if (content == null) { console.log("content为空."); return; }
        if (pwdcontent == null) { console.log("pwdcontent为空."); return; }
        if (outtxt == null) { console.log("outcontent为空."); return; }
        var inttxt = document.getElementById(content).value;
        if (inttxt === undefined || inttxt === '' || inttxt === null) { layer.msg("内容不能为空.", { icon: 2 }); return; }
        var pwd = document.getElementById(pwdcontent).value;
        if (pwd === undefined || pwd === '' || pwd === null) { layer.msg("暗号不能为空.", { icon: 2 }); return; }
        if (inttxt == null || inttxt.length < 8) {
            layer.msg("内容不能为空.", { icon: 2 }); return;
        }
        if (pwd == null || pwd.length <= 0) {
            layer.msg("暗号不能为空8.", { icon: 2 }); return;
        }
        var prand = "";
        for (var i = 0; i < pwd.length; i++) {
            prand += pwd.charCodeAt(i).toString();
        }
        var sPos = Math.floor(prand.length / 5);
        var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos * 2) + prand.charAt(sPos * 3) + prand.charAt(sPos * 4) + prand.charAt(sPos * 5));
        var incr = Math.round(pwd.length / 2);
        var modu = Math.pow(2, 31) - 1;
        var salt = parseInt(inttxt.substring(inttxt.length - 8, inttxt.length), 16);
        inttxt = inttxt.substring(0, inttxt.length - 8);
        prand += salt;
        while (prand.length > 10) {
            prand = (parseInt(prand.substring(0, 10)) + parseInt(prand.substring(10, prand.length))).toString();
        }
        prand = (mult * prand + incr) % modu;
        var enc_chr = "";
        var enc_str = "";
        for (var i = 0; i < inttxt.length; i += 2) {
            enc_chr = parseInt(parseInt(inttxt.substring(i, i + 2), 16) ^ Math.floor((prand / modu) * 255));
            enc_str += String.fromCharCode(enc_chr);
            prand = (mult * prand + incr) % modu;
        }
        enc_str = utf16to8(enc_str);
        document.getElementById(outtxt).value = enc_str;
    }function copytxt(){let area_output = document.getElementById('outtxt');var txt = area_output.value; if (txt === undefined || txt === '' || txt === null) {  layer.msg("没有输出内容.", { icon: 2 }); return; } if (XEClipboard.copy(txt)) { layer.msg("复制成功.", { icon: 1 }); } else { layer.msg("复制失败.", { icon: 2 }); } }
</script>
}
